package main.database;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import main.GlobalConfig;
import main.TimeHelper;
import org.bson.Document;
import org.bson.conversions.Bson;

import static com.mongodb.client.model.Filters.and;
import static com.mongodb.client.model.Filters.eq;

import java.util.ArrayList;
import java.util.List;

public class AudioHelper {
    /**
     * 根据提供小说的id，得到这个小说关联的所有优势小说的信息
     * @param id
     * @return
     */
    public static List<String[]> getAudioInfoFromArticleId(String id) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfAudioOfArticle);
        FindIterable<Document> findIterable = collection.find(eq("articleid", id));
        MongoCursor<Document> cursor = findIterable.iterator();

        List<String[]> resultList = new ArrayList<>();
        while (cursor.hasNext()) {
            Document doc = cursor.next();

            String[] strs = new String[3];
            strs[0] = doc.get("source").toString();
            strs[1] = doc.get("reader").toString();
            strs[2] = doc.get("createtime").toString();

            resultList.add(strs);
        }
        cursor.close();

        return resultList;
    }

    /**
     * 根据提供的小说id，演播名字，平台名字，查看同样条件的记录是否已经存在
     * @param articleId，小说id
     * @param reader，演播名字
     * @param source，平台名字
     * @return
     */
    public static boolean isAudioExist(String articleId, String reader, String source) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfAudioOfArticle);
        Bson filter = and(eq("articleid", articleId), eq("reader", reader), eq("source", source));
        return collection.countDocuments(filter) != 0;
    }

    /**，
     * 增加一个新的记录到数据库中
     * @param articleId，目标小说的id
     * @param reader，演播名字
     * @param source，平台名字
     */
    public static void addRecord(String articleId, String reader, String source) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfAudioOfArticle);

        Document document = new Document("articleid", articleId)
                .append("reader", reader)
                .append("source", source)
                .append("createtime", TimeHelper.getFormattedNowString());

        collection.insertOne(document);
    }

    public static List<String[]> findRecordsBaseOnTime(int limit) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfAudioOfArticle);
        Bson sortMethod = new Document("createtime", -1);
        FindIterable<Document> findIterable = collection.find().sort(sortMethod).limit(limit);
        MongoCursor<Document> cursor = findIterable.iterator();

        List<String[]> result = new ArrayList<>();
        while(cursor.hasNext()) {
            Document doc = cursor.next();

            String[] values = new String[4];
            values[0] = doc.get("reader").toString();
            values[1] = doc.get("source").toString();
            values[2] = doc.get("createtime").toString();

            String articleId = doc.get("articleid").toString();
            values[3] = ArticleHelper.getFieldsFromId2(articleId, true)[0];

            result.add(values);
        }
        cursor.close();

        return result;
    }
}
